﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class QuangcaoDAO:IData
 	{
		public QuangcaoDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(Quangcao item, int t) {
			 return connec.ExecuteNonQuery("sp_Quangcao", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Quangcao.c_idQuangcao, item.idQuangcao), new SqlParameter("@" + Quangcao.c_titleQuangcao, item.titleQuangcao),
				 new SqlParameter("@" + Quangcao.c_urlQuangcao, item.urlQuangcao), new SqlParameter("@" + Quangcao.c_Image, item.Image),
				 new SqlParameter("@" + Quangcao.c_orderId, item.orderId), new SqlParameter("@" + Quangcao.c_isActive, item.isActive)); 
		}

		public  Quangcao GetItem(IDataReader dr)
		{
			Quangcao obj = new Quangcao();
			obj.idQuangcao = Convert.ToInt64(dr[Quangcao.c_idQuangcao]);
			obj.titleQuangcao = Convert.ToString(dr[Quangcao.c_titleQuangcao]);
			obj.urlQuangcao = Convert.ToString(dr[Quangcao.c_urlQuangcao]);
			obj.Image = Convert.ToString(dr[Quangcao.c_Image]);
			obj.orderId = Convert.ToInt32(dr[Quangcao.c_orderId]);
			obj.isActive = Convert.ToInt32(dr[Quangcao.c_isActive]);
			return obj;
		}
        private List<Quangcao> GetAll(String strSQL, CommandType commandtype)
        {
            List<Quangcao> list = new List<Quangcao>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
		public  List<Quangcao> GetAll()
		{
			return GetAll("select * from " + Quangcao.b_Quangcao, CommandType.Text);
		}

		public  Quangcao GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.idQuangcao == (long)id);
		}

	}

 }
